package com.dean.utils;

import com.dean.entity.DataQueryEntity;
import com.dean.entity.SysDbConnInfo;
import com.dean.service.ApacheCsvOprService;
import com.dean.service.DeanExecutorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;

/**
 * 不带返回值的多线程调用
 * @author Administrator
 *
 */
public class GenCsvUtils implements Runnable {
	
	private static final Logger logger = LoggerFactory.getLogger(GenCsvUtils.class);
	
	private String fileName;
	
	private String exeSql;

	private String delimiter;

	private SysDbConnInfo connectInfo;

	public GenCsvUtils(SysDbConnInfo connectInfo, String exeSql, String fileName){
		this.fileName = fileName;
		this.exeSql = exeSql;
		this.connectInfo = connectInfo;
		this.delimiter = ",";
	}

	public GenCsvUtils(SysDbConnInfo connectInfo, String exeSql, String fileName,String delimiter){
		this.fileName = fileName;
		this.exeSql = exeSql;
		this.connectInfo = connectInfo;
		this.delimiter = delimiter==null?",":delimiter;
	}

	public  String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getExeSql() {
		return exeSql;
	}

	public void setExeSql(String exeSql) {
		this.exeSql = exeSql;
	}

	@Override
	public void run() {
		logger.info("当前线程:{},导出文件名称:{},执行sql:{},当前时间:{}",Thread.currentThread().getName(),fileName,exeSql,LocalDateTime.now());
		DataQueryEntity dataQueryEntity = DeanJdbcUtils.queryDataAndHead(connectInfo,exeSql);
		ApacheCsvOprService.getInstance().writeCsv(fileName,delimiter,dataQueryEntity.getHeader(),dataQueryEntity.getDatas());
		DeanExecutorService.consumer("csv export task sql:"+exeSql);
	}

}
